package sslsockets;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.UnrecoverableKeyException;

import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManagerFactory;

public class Client {

	public static SSLSocket createSocket(String host, int port, KeyStore trustKeyStore, KeyStore keyStore, char[] password)
			throws KeyStoreException, UnrecoverableKeyException, IOException{
		SSLSocket sslSocket = null;
		TrustManagerFactory tmf = null;
		KeyManagerFactory kmf = null;

		// create all the stuff needed to init SSLContext
		// (needed for SSLSocketFactory (needed for SSLSocket))
		try{
			tmf = TrustManagerFactory.getInstance("PKIX");
			kmf = KeyManagerFactory.getInstance("SunX509");
			tmf.init(trustKeyStore);
			kmf.init(keyStore, password);
			SecureRandom random = SecureRandom.getInstance("SHA1PRNG");

			// create, init SSLContext
    		SSLContext sslCtx = SSLContext.getInstance("SSL");
    		sslCtx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), random);
    		
    		// create SSLSocketFactory and SSLSocket
    		SSLSocketFactory sslSocketFactory = (SSLSocketFactory)sslCtx.getSocketFactory();
    		sslSocket = (SSLSocket)sslSocketFactory.createSocket(host,port);
    		
    		for (String s:sslSocket.getSupportedCipherSuites()){
    			System.out.println(s);
    		}
    		//String[] cipherSuites = {"SSL_RSA_WITH_RC4_128_SHA"};
    		String[] cipherSuites = sslSocket.getSupportedCipherSuites();
    		sslSocket.setEnabledCipherSuites(cipherSuites);

		}
		catch (NoSuchAlgorithmException e){
			e.printStackTrace();
			System.exit(1);
		} catch (KeyManagementException e) {
			e.printStackTrace();
			System.exit(1);
		}
		
		return sslSocket;
	}

	
	public static void main(String[] args){
		File trustKeyStoreFile = new File("/home/michau/Desktop/Michau/programy/Bsk/bsk-proj/src/server/config/PKWCert");
		File keyStoreFile = new File("/home/michau/Desktop/90102611831");
		FileInputStream fis = null;
		try{
			KeyStore trustKeyStore = KeyStore.getInstance(KeyStore.getDefaultType());
			KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
			
			fis = new FileInputStream(trustKeyStoreFile);
			trustKeyStore.load(fis, "".toCharArray());
			
			fis.close();
			fis = new FileInputStream(keyStoreFile);
			keyStore.load(fis, "asd".toCharArray());
			
			SSLSocket sslSocket = createSocket("localhost", 1234, trustKeyStore, keyStore, "asd".toCharArray());
			
    		System.out.println("We've got a socket. And connected too!");
    		
			OutputStream out = sslSocket.getOutputStream();
			ObjectOutputStream oout = new ObjectOutputStream(out);
    	    
    	    //PrintWriter out = new PrintWriter(sslSocket.getOutputStream(), true);
    	    
    	    BufferedReader in = new BufferedReader(new InputStreamReader(
                    sslSocket.getInputStream()));
    	    
    	    BufferedReader stdIn = new BufferedReader(
                    new InputStreamReader(System.in));
		    
		    String userInput;
		    while ((userInput = stdIn.readLine()) != null) {
		        oout.writeObject(userInput);
		        System.out.println("echo: " + in.readLine());
		    }
		    
		    out.close();
		    sslSocket.close();
		    stdIn.close();
		    fis.close();

		} catch (Exception e){
			e.printStackTrace();
		}
	}
}
